<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>寄生组合式继承</title>
</head>
<body>
  
</body>
<script>
  function Animal(name) {
    this.name = name;
    this.colors = ['red', 'green', 'blue'];
  }
  Animal.prototype.getName = function () {
    return this.name;
  }

  function Dog(name) {
    // 让父类的实例属性继承下来，实例修改引用类型的值，另一个实例的引用类型的值不会发生变化
    Animal.call(this, name);
  };

  // 重写原型对象：把父类的共享方法继承下来
  Dog.prototype = Object.create(Animal.prototype);
  Dog.prototype.constructor = Dog;

  var d1 = new Dog('大黄');
  var d2 = new Dog('小红');

  d1.colors.push('pink');
  console.log(d1.name);  // 大黄
  console.log(d2.name);  // 小红
  console.log(d1.colors);  // ["red", "green", "blue", "pink"]
  console.log(d2.colors);  // ["red", "green", "blue"]
  console.log(d1.getName()) // 大黄
  console.log(d2.getName()) // 小红

</script>
</html>